﻿Ext.require([
    'BIM.store.OptionType',
    'Twi.Msg',
    'Twi.Tbar',
    'Twi.QuickAjax',
    'Twi.QuickGrid',
    'Twi.QuickWin',
    'TAB.view.option.OptionNewOrEditForm',
    'TAB.view.option.OptionTypeNewOrEditForm'
]);

Ext.onReady(function () {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    twiSelf.init();
});

Twi.TAB.Option = {
    Vars:
    {
        optionGrid: null,
        optionTypeGrid: null,
        optionMappingName: 'SM004',
        optionTypeMappingName: 'SM003'
    },

    init: function () {
        //Twi.Msg.Info('提示', 'Hello Xuzhihong!');
        twiVars.optionTypeGrid = twiSelf.Grid.createOptionTypeGrid();
        twiVars.optionGrid = twiSelf.Grid.createOptionGrid();
    }

    , Grid:
    {
        createOptionTypeGrid: function () {
            var store = Ext.create("BIM.store.OptionType");
            store.on('load', function (s) {
                //解决grid.store.load()后，默认选择的行其实是load之前选择的脏数据
                var grid = twiVars.optionTypeGrid;
                var lastSltRecord = grid.getSelectionModel().getLastSelected();
                if (lastSltRecord) {
                    grid.getSelectionModel().deselectAll();
                    var sltRecord = s.getById(lastSltRecord.getId());
                    if (sltRecord) {
                        grid.getSelectionModel().select(sltRecord);
                    }
                }
            });
            var actionItems = twiSelf.ActionItems.createOptionTypeItems();
            var contextMenu = Ext.create('Ext.menu.Menu', {
                items: actionItems
            });
            var tbarQuery = Twi.Tbar.Query('名称：', twiSelf.Fn.OptionType.btnQuery);
            var grid = Ext.create("Twi.QuickGrid", {
                title: '选项类别',
                //forceFit:true,
                store: store,
                renderTo: 'optionTypeGrid',
                dockedItems: [{
                    xtype: 'toolbar',
                    items: actionItems.concat('->', tbarQuery)
                }],
                viewConfig: {
                    stripeRows: true,
                    listeners: {
                        itemcontextmenu: function (view, rec, node, index, e) {
                            e.stopEvent();
                            contextMenu.showAt(e.getXY());
                            return false;
                        }
                    }
                },
                columns: [{ xtype: 'rownumberer', width: 30 },
                 {
                     header: '名称',
                     dataIndex: 'FNAME',
                     flex: 1
                 }, {
                     header: '排序',
                     dataIndex: 'FSORT'
                 }, {
                     header: '备注',
                     dataIndex: 'FREMARK',
                     flex: 1
                 }]
            });

            grid.on('itemclick', function (view, record) {
                twiVars.optionGrid.store.proxy.extraParams.data = Ext.encode({
                    TableName: twiVars.optionMappingName,
                    OrderFields: 'FSORT',
                    Condition:
                    {
                        FIsDeleted: 0,
                        FOptionTypeID: record.data.FID
                    }
                });
                twiVars.optionGrid.store.load();
            });
            grid.on('itemdblclick', function () {
                if (!this.dockedItems.items[2].items.items[2].disabled) {
                    this.dockedItems.items[2].items.items[2].handler();
                }
            });

            return grid;

        } //end createOptionTypeGrid

        , createOptionGrid: function () {
            var store = Ext.create("BIM.store.Option");
            store.on('load', function (s) {
                //解决grid.store.load()后，默认选择的行其实是load之前选择的脏数据
                var grid = twiVars.optionGrid;
                var lastSltRecord = grid.getSelectionModel().getLastSelected();
                if (lastSltRecord) {
                    grid.getSelectionModel().deselectAll();
                    var sltRecord = s.getById(lastSltRecord.getId());
                    if (sltRecord) {
                        grid.getSelectionModel().select(sltRecord);
                    }
                }
            });
            var actionItems = twiSelf.ActionItems.createOptionItems();
            var contextMenu = Ext.create('Ext.menu.Menu', {
                items: actionItems
            });
            var tbarQuery = Twi.Tbar.Query('名称：', twiSelf.Fn.Option.btnQuery);
            var selModel = Ext.create('Ext.selection.CheckboxModel');
            var grid = Ext.create("Twi.QuickGrid", {
                title: '选项',
                //forceFit: true,
                store: store,
                selModel: selModel,
                renderTo: 'optionGrid',
                dockedItems: [{
                    xtype: 'toolbar',
                    items: actionItems.concat('->', tbarQuery)
                }],
                viewConfig: {
                    stripeRows: true,
                    listeners: {
                        itemcontextmenu: function (view, rec, node, index, e) {
                            e.stopEvent();
                            contextMenu.showAt(e.getXY());
                            return false;
                        }
                    }
                },
                columns: [{ xtype: 'rownumberer', width: 30 },
                 {
                     header: '名称',
                     dataIndex: 'FNAME',
                     flex: 1
                 }, {
                     header: '排序',
                     dataIndex: 'FSORT'

                 }, {
                     header: '备注',
                     dataIndex: 'FREMARK',
                     flex: 1
                 }]
            });
            grid.on('itemdblclick', function () {
                if (!this.dockedItems.items[2].items.items[2].disabled) {
                    this.dockedItems.items[2].items.items[2].handler();
                }
            });
            return grid;
        } //end createOptionGrid

    } //end Grid

    , ActionItems: {
        createOptionTypeItems: function () {
            var items = [];
            items.push({
                text: '新增',
                iconCls: 'ico_add',
                handler: twiSelf.Fn.OptionType.btnNew
            }, '-', {
                text: '编辑',
                iconCls: 'ico_edit',
                handler: twiSelf.Fn.OptionType.btnEdit
            }, '-', {
                text: '删除',
                iconCls: 'ico_delete',
                handler: twiSelf.Fn.OptionType.btnDelete
            }, '-', {
                text: '刷新',
                iconCls: 'ico_refresh',
                handler: twiSelf.Fn.OptionType.btnRefresh
            });
            return items;
        } //end createOptionTypeItems

        , createOptionItems: function () {
            var items = [];
            items.push({
                text: '新增',
                iconCls: 'ico_add',
                handler: twiSelf.Fn.Option.btnNew
            }, '-', {
                text: '编辑',
                iconCls: 'ico_edit',
                handler: twiSelf.Fn.Option.btnEdit
            }, '-', {
                text: '删除',
                iconCls: 'ico_delete',
                handler: twiSelf.Fn.Option.btnDelete
            }, '-', {
                text: '刷新',
                iconCls: 'ico_refresh',
                handler: twiSelf.Fn.Option.btnRefresh
            });
            return items;
        } //end createOptionItems

    }//end ActionItems

    , Win: {
        createOptionTypeWin: function () {
            var form = Ext.create('TAB.view.option.OptionTypeNewOrEditForm', { border: false });
            var win = Ext.create('Twi.QuickWin', {
                title: '窗口',
                width: 300,
                //x: 140,
                //y: 100,
                items: [form]
            });
            return { win: win,
                form: form
            }
        } //end createOptionTypeWin

            , createOptionWin: function () {
                var form = Ext.create('TAB.view.option.OptionNewOrEditForm', { border: false });
                var win = Ext.create('Twi.QuickWin', {
                    title: '窗口',
                    width: 300,
                    x: 340,
                    y: 100,
                    items: [form]
                });
                return { win: win,
                    form: form
                }
            } //end createOptionWin

    } //end win
    , Fn:
    {
        OptionType:
        {
            newOrEditCommit: function (win, newOrEdit) {
                var form = win.form.form;
                var btnCommit = win.win.down('#btnSave');
                btnCommit.clearListeners();
                btnCommit.on('click', function () {
                    if (form.isValid()) {
                        btnCommit.disable();
                        var record = form.getValues();
                        var data = {
                            TableName: twiVars.optionTypeMappingName,
                            NewOrEdit: newOrEdit,
                            Fields: record,
                            SortModel: {
                                SortField: 'FSort'
                            }
                        };
                        if ("Edit" == newOrEdit) {
                            data.Condition =
                            {
                                FID: record.FID
                            }
                            delete data.Fields.FID;
                        }

                        Twi.QuickAjax.Ajax({
                            params: {
                                cn: 'PageCommon',
                                mn: 'TAB',
                                mn2: 'NewOrEdit',
                                data: Ext.encode(data)
                            },
                            success: function (obj) {
                                win.win.close();
                                twiSelf.Vars.optionTypeGrid.store.load();
                            }
                        });
                    }
                });
            } //end newOrEditCommit

            , btnNew: function () {
                if (!Twi.IsDeveloper) {
                    Twi.Msg.Error('', Twi.NotDeveloperError);
                    return;
                }
                var win = twiSelf.Win.createOptionTypeWin();
                win.win.setTitle('新增选项类别');
                twiSelf.Fn.OptionType.newOrEditCommit(win, 'New');
                win.win.show();
            }
            , btnEdit: function () {
                if (!Twi.IsDeveloper) {
                    Twi.Msg.Error('', Twi.NotDeveloperError);
                    return;
                }
                var len = twiVars.optionTypeGrid.selModel.selected.length;
                if (len > 0) {
                    var win = twiSelf.Win.createOptionTypeWin();
                    win.win.setTitle('编辑选项类别');
                    var record = twiVars.optionTypeGrid.selModel.selected.items[len - 1];
                    win.form.loadRecord(record);
                    twiSelf.Fn.OptionType.newOrEditCommit(win, 'Edit');
                    win.win.show();
                }
                else {
                    Twi.Msg.Info('温馨提示', '请选择一项进行编辑');
                }

            }
            , btnDelete: function () {
                if (!Twi.IsDeveloper) {
                    Twi.Msg.Error('', Twi.NotDeveloperError);
                    return;
                }
                var len = twiVars.optionTypeGrid.selModel.selected.length;
                if (len > 0) {                    
                    Twi.Msg.Confirm(function () {
                        var record = twiVars.optionTypeGrid.selModel.selected.items[len - 1];
                        var data = {
                            TableName: twiVars.optionTypeMappingName,
                            Fields: {
                                FIsDeleted: 1,
                                FSort: ''
                            },
                            Condition: {
                                FID: record.data.FID
                            }
                        };
                        Twi.QuickAjax.Ajax({
                            params: {
                                cn: 'PageCommon',
                                mn: 'TAB',
                                mn2: 'SimpleEdit',
                                data: Ext.encode(data)
                            },
                            success: function () {
                                twiVars.optionTypeGrid.store.load();
                            }
                        });
                    });

                }
                else {
                    Twi.Msg.Info('温馨提示', '请选择一项进行删除');
                }
            } //end btnDelete

            , btnRefresh: function () {
                Ext.ComponentQuery.query('#txtContent', twiVars.optionTypeGrid)[0].setValue("");
                var data = {
                    TableName: twiVars.optionTypeMappingName,
                    OrderFields: 'FSORT',
                    Condition:
                    {
                        FIsDeleted: 0
                    }
                };
                twiVars.optionTypeGrid.store.proxy.extraParams.data = Ext.encode(data);
                twiVars.optionTypeGrid.store.load();

            } //end btnRefresh

            , btnQuery: function () {
                var content = Ext.ComponentQuery.query('#txtContent', twiVars.optionTypeGrid)[0].value;
                var data = {
                    TableName: twiVars.optionTypeMappingName,
                    OrderFields: 'FSORT',
                    Condition:
                    {
                        FIsDeleted: 0
                    }
                };
                if (content) {
                    data.SimpleCondition = {
                        Field: "FName",
                        Value: content,
                        COperator: 'Like'
                    };
                }
                twiVars.optionTypeGrid.store.proxy.extraParams.data = Ext.encode(data);
                twiVars.optionTypeGrid.store.load();

            } //end btnQuery
        }
        , Option:
        {
            newOrEditCommit: function (win, newOrEdit) {
                var form = win.form.form;
                var btnCommit = win.win.down('#btnSave');
                btnCommit.clearListeners();
                btnCommit.on('click', function () {
                    if (form.isValid()) {
                        btnCommit.disable();
                        var record = form.getValues();
                        var data = {
                            TableName: twiVars.optionMappingName,
                            NewOrEdit: newOrEdit,
                            Fields: record,
                            SortModel: {
                                SortField: 'FSort',
                                FilterField: 'FOptionTypeID'
                            }
                        };
                        if ("Edit" == newOrEdit) {
                            data.Condition =
                            {
                                FID: record.FID
                            }
                            delete data.Fields.FID;
                        }
                        Twi.QuickAjax.Ajax({
                            params: {
                                cn: 'PageCommon',
                                mn: 'TAB',
                                mn2: 'NewOrEdit',
                                data: Ext.encode(data)
                            },
                            success: function (obj) {
                                win.win.close();
                                twiSelf.Vars.optionGrid.store.load();                                
                            }
                        });
                    }
                });
            } //end newOrEditCommit

            , btnNew: function () {

                var lenType = twiVars.optionTypeGrid.selModel.selected.length;
                if (lenType > 0) {
                    var win = twiSelf.Win.createOptionWin();
                    win.win.setTitle('新增选项');
                    var record = twiVars.optionTypeGrid.selModel.selected.items[lenType - 1];
                    win.form.form.findField("FOPTIONTYPEID").setValue(record.data.FID);
                    win.form.form.findField("TypeName").setValue(record.data.FNAME);
                    twiSelf.Fn.Option.newOrEditCommit(win, 'New');
                    win.win.show();
                }
                else {
                    Twi.Msg.Info('', "请从左边选择一个要新增选项的类别");
                }
            } //end btnNew

            , btnEdit: function () {
                var lenType = twiVars.optionTypeGrid.selModel.selected.length;
                if (lenType > 0) {
                    var len = twiVars.optionGrid.selModel.selected.length;
                    if (len > 0) {
                        var win = twiSelf.Win.createOptionWin();
                        win.win.setTitle('编辑选项');
                        var record = twiVars.optionGrid.selModel.selected.items[len - 1];
                        var recordType = twiVars.optionTypeGrid.selModel.selected.items[lenType - 1];
                        win.form.loadRecord(record);
                        win.form.form.findField("FOPTIONTYPEID").setValue(recordType.data.FID);
                        win.form.form.findField("TypeName").setValue(recordType.data.FNAME);
                        twiSelf.Fn.Option.newOrEditCommit(win, 'Edit');
                        win.win.show();
                    }
                    else {
                        Twi.Msg.Info('温馨提示', '请选择一项进行编辑');
                    }
                }
            } //end btnEdit

            , btnDelete: function () {
                var len = twiVars.optionGrid.selModel.selected.length;
                if (len > 0) {
                    Twi.Msg.Confirm(function () {
                        var IDs = [];
                        Ext.each(twiVars.optionGrid.selModel.selected.items, function (item, index, allItems) {
                            IDs.push(item.data.FID);
                        });
                        var record = twiVars.optionGrid.selModel.selected.items[len - 1];
                        var data = {
                            TableName: twiVars.optionMappingName,
                            IDs: IDs.join(',')
                        };
                        Twi.QuickAjax.Ajax({
                            params: {
                                cn: 'PageCommon',
                                mn: 'TAB',
                                mn2: 'DeleteBySetFlag',
                                data: Ext.encode(data)
                            },
                            success: function () {
                                twiVars.optionGrid.store.load();
                            }
                        });
                    });

                }
                else {
                    Twi.Msg.Info('温馨提示', '请选择一项进行删除');
                }
            } //end btnDelete

             , btnRefresh: function () {
                 var lenType = twiVars.optionTypeGrid.selModel.selected.length;
                 var record = twiVars.optionTypeGrid.selModel.selected.items[lenType - 1];
                 Ext.ComponentQuery.query('#txtContent', twiVars.optionGrid)[0].setValue("");
                 var data = {
                     TableName: twiVars.optionMappingName,
                     OrderFields: 'FSORT',
                     Condition:
                    {
                        FIsDeleted: 0,
                        FOptionTypeID: record.data.FID
                    }
                 };
                 twiVars.optionGrid.store.proxy.extraParams.data = Ext.encode(data);
                 twiVars.optionGrid.store.load();

             } //end btnRefresh

            , btnQuery: function () {
                var lenType = twiVars.optionTypeGrid.selModel.selected.length;
                var record = twiVars.optionTypeGrid.selModel.selected.items[lenType - 1];
                var content = Ext.ComponentQuery.query('#txtContent', twiVars.optionGrid)[0].value;
                var data = {
                    TableName: twiVars.optionMappingName,
                    OrderFields: 'FSORT',
                    Condition:
                    {
                        FIsDeleted: 0,
                        FOptionTypeID: record.data.FID
                    }
                };
                if (content) {
                    data.SimpleCondition = {
                        Field: "FName",
                        Value: content,
                        COperator: 'Like'
                    };
                }
                twiVars.optionGrid.store.proxy.extraParams.data = Ext.encode(data);
                twiVars.optionGrid.store.load();

            } //end btnQuery
        }
    } //end Fn

};

var twiSelf = Twi.TAB.Option;
var twiVars = twiSelf.Vars;